package com.example.musicaiap.ui.components

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.performClick
import androidx.media3.common.MediaItem
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.musicaiap.domain.model.Song
import com.example.musicaiap.service.toMediaItem
import com.example.musicaiap.service.toMediaItems
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class PlaylistBottomSheetTest {
    
    @get:Rule
    val composeTestRule = createComposeRule()
    
    @Test
    fun test_playlist_bottom_sheet_displays_song_info() {
        // 准备测试数据
        val songs = listOf(
            Song(
                id = "test_id_1",
                title = "Test Song 1",
                artist = "Test Artist 1",
                album = "Test Album 1",
                duration = 1000L,
                coverUrl = "http://test.com/cover1.jpg",
                url = "http://test.com/song1.mp3",
                lyricUrl = null,
                isVip = false
            ),
            Song(
                id = "test_id_2",
                title = "Test Song 2",
                artist = "Test Artist 2",
                album = "Test Album 2",
                duration = 2000L,
                coverUrl = "http://test.com/cover2.jpg",
                url = "http://test.com/song2.mp3",
                lyricUrl = null,
                isVip = true
            )
        )
        
        val mediaItems = songs.toMediaItems()
        val currentSong = songs[0]
        
        // 设置 Composable
        composeTestRule.setContent {
            PlaylistBottomSheet(
                isVisible = true,
                onDismiss = {},
                playQueue = mediaItems,
                currentSong = currentSong,
                onSongClick = {}
            )
        }
        
        // 验证UI显示
        composeTestRule.onNodeWithText("Test Song 1").assertIsDisplayed()
        composeTestRule.onNodeWithText("Test Artist 1").assertIsDisplayed()
        composeTestRule.onNodeWithText("Test Song 2").assertIsDisplayed()
        composeTestRule.onNodeWithText("Test Artist 2").assertIsDisplayed()
    }
    
    @Test
    fun test_playlist_bottom_sheet_click_song() {
        // 准备测试数据
        val songs = listOf(
            Song(
                id = "test_id_1",
                title = "Test Song 1",
                artist = "Test Artist 1",
                album = "Test Album 1",
                duration = 1000L,
                coverUrl = "http://test.com/cover1.jpg",
                url = "http://test.com/song1.mp3",
                lyricUrl = null,
                isVip = false
            )
        )
        
        val mediaItems = songs.toMediaItems()
        val currentSong = songs[0]
        var clickedSong: Song? = null
        
        // 设置 Composable
        composeTestRule.setContent {
            PlaylistBottomSheet(
                isVisible = true,
                onDismiss = {},
                playQueue = mediaItems,
                currentSong = currentSong,
                onSongClick = { song -> clickedSong = song }
            )
        }
        
        // 点击歌曲
        composeTestRule.onNodeWithText("Test Song 1").performClick()
        
        // 验证点击回调
        assert(clickedSong == songs[0])
    }
} 